/*
 * Main.fx
 *
 * Created on 25.10.2009, 20:45:07
 */

package justforfun.bombsquare;

import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import justforfun.bombsquare.Square;
import justforfun.bombsquare.ControlSquareBind;
import justforfun.bombsquare.Constants.*;
import justforfun.bombsquare.BoomSquare;


/**
 * @author civil
 */

//возможно когда нибудь это станет бомберменом :3
//но пока тут только бессмертный жёлтый, квадрату уничтожающий зелёные квадраты с помощью красных ^^

var field: myNode[] = [];

function squares (height: Integer, width: Integer): myNode[] {
    for (y in [0..(height / my)]) {
        for (x in [0..(width / mx)]) {
            var canBeEmploded: Boolean = if (x mod 2 == 0 or y mod 2 == 0)
                                    true
                               else
                                    false;

            var square: Square = Square {
                x: x
                y: y
                canBeExploded: canBeEmploded
                vincible: false

                fill: if (canBeEmploded) Color.GREEN else Color.BLACK
            }
            square
        }
    }
}


field = squares(16 * my, 16 * mx);

field = field[obj | not (obj.x == 0 and obj.y >= 0 and obj.y < 3)];


var bs: Square = Square {
    x: 0
    y: 0
    fill: Color.YELLOW
}

var control = ControlSquareBind {
    action: function () {
        var boom: BoomSquare = BoomSquare {
            action: function () {
                var expl = function(x: Float, y: Float) {
                    field = field[obj | not (obj.x == x and obj.y == y and obj.canBeExploded)];
                    if (field[obj | (obj.x == x and obj.y == y and not obj.canBeExploded)].size() == 0) {
                        var bar: BoomSquare = BoomSquare {
                            x: x
                            y: y
                            waitTime: 6s
                            action: function () {
                                delete bar from field
                            }
                        }
                        insert bar into field;
                    }
                };

                expl(boom.x, boom.y + 1);
                expl(boom.x, boom.y - 1);
                expl(boom.x + 1, boom.y);
                expl(boom.x - 1, boom.y);
                delete boom from field;
            }
            waitTime: 6s
            x: bs.x
            y: bs.y
        }

        insert boom into field;
    }

    field: bind field
    square: bind bs
    height: 16 * my
    width: 16 * mx
}

Stage {
    height: 16 * my
    width: 16 * mx

    scene: Scene {
        content: bind [field, bs, control]
    }
}